package com.example.chatroom.control;

import cn.hutool.core.collection.CollUtil;
import com.example.chatroom.context.UserHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/chat")
public class ChatController {
//这里为了方便就没有写service层了
    @GetMapping("/join")
    public void joinRoom(String roomId,String userId){
if (!UserHolder.MAP.containsKey(roomId)){
    UserHolder.MAP.put(roomId,new ArrayList<>());
}

            List<String> userIdList = UserHolder.MAP.get(roomId);
            if (CollUtil.isEmpty(userIdList)){
                System.out.println("聊天室第一个人");
                userIdList.add(userId);
                 return;
            }
            String exceptSelfListStr = userIdList.toString();

            userIdList.add(userId);
            System.out.println(exceptSelfListStr);

    }

    @GetMapping("/leave")
    public void leaveRoom(String roomId,String userId){
        if (!UserHolder.MAP.containsKey(roomId)){

        }
        ArrayList<String> userIds = UserHolder.MAP.get(roomId);
        userIds.remove(userId);

        if (CollUtil.isEmpty(userIds)){
            System.out.println("聊天室最后一个人");
return;
        }
        String exceptSelfListStr = userIds.toString();
        System.out.println(exceptSelfListStr);


    }
}
